package com.uzai.console.service.wechatwork.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.dto.wxwork.DelWxWorkChannelQrCodeDto;
import com.uzai.common.entity.WxworkUser;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.dto.wechatwork.channelqrcode.WxworkChannelQrcodeIdDto;
import com.uzai.console.dto.wechatwork.channelqrcode.WxworkChannelQrcodeQuery;
import com.uzai.console.entity.DeviceWxworkInfo;
import com.uzai.console.entity.WxworkChannelQrcode;
import com.uzai.console.mapper.DeviceWxworkInfoMapper;
import com.uzai.console.mapper.WxworkChannelQrcodeMapper;
import com.uzai.console.mapper.WxworkUserMapper;
import com.uzai.console.service.feign.WxContactsFeignService;
import com.uzai.console.service.wechatwork.WxworkChannelQrcodeService;
import com.uzai.console.vo.wechatwork.channelqrcode.WxworkChannelQrcodeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 企业微信推广员
 * @author liuqi
 * @date 2021年08月19日 16:50
 */
@Service
public class WxworkChannelQrcodeServiceImpl implements WxworkChannelQrcodeService {

    Logger logger = LoggerFactory.getLogger(WxworkChannelQrcodeServiceImpl.class);

    @Autowired
    private WxworkChannelQrcodeMapper wxworkChannelQrcodeMapper;
    @Autowired
    private WxworkUserMapper wxworkUserMapper;
    @Autowired
    private DeviceWxworkInfoMapper deviceWxworkInfoMapper;
    @Autowired
    private WxContactsFeignService wxContactsFeignService;

    /**
     * 查询列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxworkChannelQrcodeQuery
     * @return null
     */
    public Page<WxworkChannelQrcodeVo> findWxworkChannelQrcodePage(WxworkChannelQrcodeQuery wxworkChannelQrcodeQuery){

        //新建返回到页面的LIST对象
        List<WxworkChannelQrcodeVo> wxworkChannelQrcodeVoList = new ArrayList<>();

        //执行分页查询对象
        Page<WxworkChannelQrcode> page = new Page<>();
        wxworkChannelQrcodeQuery.convert(page);
        ArrayList<WxworkChannelQrcode> wxworkChannelQrcodeList =  wxworkChannelQrcodeMapper.findByList(page, wxworkChannelQrcodeQuery);
        if(wxworkChannelQrcodeList != null && wxworkChannelQrcodeList.size() > 0){
            for (WxworkChannelQrcode wxworkChannelQrcode : wxworkChannelQrcodeList){
                WxworkChannelQrcodeVo wxworkChannelQrcodeVo = new WxworkChannelQrcodeVo();
                BeanUtils.copyProperties(wxworkChannelQrcode, wxworkChannelQrcodeVo);
                wxworkChannelQrcodeVoList.add(wxworkChannelQrcodeVo);

                //企业微信推广员
                if(wxworkChannelQrcode.getUserId() != null){
                    WxworkUser wxworkUser = wxworkUserMapper.selectById(wxworkChannelQrcode.getUserId(), wxworkChannelQrcode.getMerId());
                    if(wxworkUser != null){
                        wxworkChannelQrcodeVo.setBizUserId(wxworkUser.getBizUserId());
                        wxworkChannelQrcodeVo.setUserName(wxworkUser.getNickName());
                        wxworkChannelQrcodeVo.setUserRemark(wxworkUser.getRemark());
                    }
                }

                //企业微信机器人
                if(wxworkChannelQrcode.getDeviceUniqueId() != null){
                    DeviceWxworkInfo deviceWxworkInfo = deviceWxworkInfoMapper.selectById(wxworkChannelQrcode.getDeviceUniqueId(), wxworkChannelQrcode.getMerId());
                    if(deviceWxworkInfo != null){
                        wxworkChannelQrcodeVo.setDeviceId(deviceWxworkInfo.getDeviceId());
                        wxworkChannelQrcodeVo.setDeviceName(deviceWxworkInfo.getDeviceName());
                        wxworkChannelQrcodeVo.setDeviceRemark(deviceWxworkInfo.getDeviceRemark());
                    }
                }
            }
        }
        //返回到页面分页对象
        Page<WxworkChannelQrcodeVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(wxworkChannelQrcodeVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 删除
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param wxworkChannelQrcodeIdDto
     * @return null
     */
    public void deleteWxworkChannelQrcode(WxworkChannelQrcodeIdDto wxworkChannelQrcodeIdDto){
        if(wxworkChannelQrcodeIdDto.getId() == null ){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择id");
        }

        WxworkChannelQrcode wxworkChannelQrcode = wxworkChannelQrcodeMapper.selectById(wxworkChannelQrcodeIdDto.getId(), wxworkChannelQrcodeIdDto.getMerId());
        if(wxworkChannelQrcode == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该推广员不存在");
        }

        if(StringUtils.isBlank(wxworkChannelQrcode.getConfigId())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该推广员的二维码配置id不存在");
        }
        
        //调用微服务远程删除
        DelWxWorkChannelQrCodeDto delWxWorkChannelQrCodeDto = new DelWxWorkChannelQrCodeDto();
        delWxWorkChannelQrCodeDto.setConfigId(wxworkChannelQrcode.getConfigId());
        delWxWorkChannelQrCodeDto.setMerId(wxworkChannelQrcode.getMerId());
        delWxWorkChannelQrCodeDto.setDeviceUniqueId(wxworkChannelQrcode.getDeviceUniqueId());
        delWxWorkChannelQrCodeDto.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT_VALUE);
        UzaiRespVo<Boolean> uzaiRespVo = wxContactsFeignService.wxworkDelChanelQrcode(delWxWorkChannelQrCodeDto);
        logger.info("--调用删除企业微信推广员接口---dto={},result={}", JSONObject.toJSONString(delWxWorkChannelQrCodeDto), JSONObject.toJSONString(uzaiRespVo));
        if(uzaiRespVo != null && uzaiRespVo.getCode() == 200 && uzaiRespVo.getData()){
            //调用成功，才执行删除功能
            wxworkChannelQrcodeMapper.deleteById(wxworkChannelQrcodeIdDto.getId(), wxworkChannelQrcodeIdDto.getMerId());
        }else{
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "远程调用删除推广员接口失败");
        }
    }


}
